{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting efficient_apriori\n",
      "  Downloading https://files.pythonhosted.org/packages/5a/c6/ecdf3a32d23cada466634c649cf4f50fefe76f56eae53ecceff688b306be/efficient_apriori-1.1.1-py3-none-any.whl\n",
      "Installing collected packages: efficient-apriori\n",
      "Successfully installed efficient-apriori-1.1.1\n"
     ]
    }
   ],
   "source": [
    "!pip install efficient_apriori"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              0          1           2                 3             4   \\\n",
      "0         shrimp    almonds     avocado    vegetables mix  green grapes   \n",
      "1        burgers  meatballs        eggs               NaN           NaN   \n",
      "2        chutney        NaN         NaN               NaN           NaN   \n",
      "3         turkey    avocado         NaN               NaN           NaN   \n",
      "4  mineral water       milk  energy bar  whole wheat rice     green tea   \n",
      "\n",
      "                 5     6               7             8             9   \\\n",
      "0  whole weat flour  yams  cottage cheese  energy drink  tomato juice   \n",
      "1               NaN   NaN             NaN           NaN           NaN   \n",
      "2               NaN   NaN             NaN           NaN           NaN   \n",
      "3               NaN   NaN             NaN           NaN           NaN   \n",
      "4               NaN   NaN             NaN           NaN           NaN   \n",
      "\n",
      "               10         11     12     13             14      15  \\\n",
      "0  low fat yogurt  green tea  honey  salad  mineral water  salmon   \n",
      "1             NaN        NaN    NaN    NaN            NaN     NaN   \n",
      "2             NaN        NaN    NaN    NaN            NaN     NaN   \n",
      "3             NaN        NaN    NaN    NaN            NaN     NaN   \n",
      "4             NaN        NaN    NaN    NaN            NaN     NaN   \n",
      "\n",
      "                  16               17       18         19  \n",
      "0  antioxydant juice  frozen smoothie  spinach  olive oil  \n",
      "1                NaN              NaN      NaN        NaN  \n",
      "2                NaN              NaN      NaN        NaN  \n",
      "3                NaN              NaN      NaN        NaN  \n",
      "4                NaN              NaN      NaN        NaN  \n",
      "(7501, 20)\n",
      "['chutney']\n",
      "频繁项集: {1: {('frozen smoothie',): 475, ('tomato juice',): 228, ('olive oil',): 494, ('vegetables mix',): 193, ('energy drink',): 200, ('low fat yogurt',): 574, ('cottage cheese',): 239, ('green tea',): 991, ('almonds',): 153, ('avocado',): 250, ('honey',): 356, ('shrimp',): 536, ('salmon',): 319, ('mineral water',): 1788, ('burgers',): 654, ('meatballs',): 157, ('eggs',): 1348, ('turkey',): 469, ('milk',): 972, ('whole wheat rice',): 439, ('energy bar',): 203, ('french fries',): 1282, ('whole wheat pasta',): 221, ('soup',): 379, ('spaghetti',): 1306, ('frozen vegetables',): 715, ('cookies',): 603, ('cooking oil',): 383, ('champagne',): 351, ('chocolate',): 1229, ('oil',): 173, ('chicken',): 450, ('fresh tuna',): 167, ('tomatoes',): 513, ('red wine',): 211, ('pepper',): 199, ('pancakes',): 713, ('ham',): 199, ('grated cheese',): 393, ('fresh bread',): 323, ('escalope',): 595, ('ground beef',): 737, ('herb & pepper',): 371, ('strawberries',): 160, ('cake',): 608, ('hot dogs',): 243, ('brownies',): 253, ('cereals',): 193, ('muffins',): 181, ('light mayo',): 204, ('yogurt cake',): 205, ('butter',): 226, ('french wine',): 169}, 2: {('frozen smoothie', 'mineral water'): 152, ('green tea', 'mineral water'): 233, ('low fat yogurt', 'mineral water'): 180, ('mineral water', 'olive oil'): 207, ('mineral water', 'shrimp'): 177, ('burgers', 'eggs'): 216, ('milk', 'mineral water'): 360, ('mineral water', 'whole wheat rice'): 151, ('frozen vegetables', 'spaghetti'): 209, ('green tea', 'spaghetti'): 199, ('burgers', 'mineral water'): 183, ('cooking oil', 'mineral water'): 151, ('eggs', 'mineral water'): 382, ('chicken', 'mineral water'): 171, ('eggs', 'spaghetti'): 274, ('mineral water', 'spaghetti'): 448, ('mineral water', 'tomatoes'): 183, ('spaghetti', 'tomatoes'): 157, ('french fries', 'milk'): 178, ('chocolate', 'eggs'): 249, ('mineral water', 'pancakes'): 253, ('pancakes', 'spaghetti'): 189, ('milk', 'spaghetti'): 266, ('ground beef', 'milk'): 165, ('ground beef', 'mineral water'): 307, ('ground beef', 'spaghetti'): 294, ('chocolate', 'french fries'): 258, ('chocolate', 'mineral water'): 395, ('eggs', 'french fries'): 273, ('french fries', 'mineral water'): 253, ('frozen vegetables', 'mineral water'): 268, ('chocolate', 'frozen vegetables'): 172, ('cake', 'mineral water'): 206, ('french fries', 'green tea'): 214, ('french fries', 'pancakes'): 151, ('chocolate', 'green tea'): 176, ('chocolate', 'spaghetti'): 294, ('chocolate', 'ground beef'): 173, ('chocolate', 'milk'): 241, ('frozen vegetables', 'milk'): 177, ('mineral water', 'soup'): 173, ('olive oil', 'spaghetti'): 172, ('burgers', 'french fries'): 165, ('burgers', 'spaghetti'): 161, ('french fries', 'spaghetti'): 207, ('shrimp', 'spaghetti'): 159, ('eggs', 'green tea'): 191, ('eggs', 'milk'): 231, ('eggs', 'pancakes'): 163, ('eggs', 'frozen vegetables'): 163}}\n",
      "关联规则: [{frozen smoothie} -> {mineral water}, {low fat yogurt} -> {mineral water}, {olive oil} -> {mineral water}, {shrimp} -> {mineral water}, {burgers} -> {eggs}, {milk} -> {mineral water}, {whole wheat rice} -> {mineral water}, {cooking oil} -> {mineral water}, {chicken} -> {mineral water}, {spaghetti} -> {mineral water}, {tomatoes} -> {mineral water}, {tomatoes} -> {spaghetti}, {pancakes} -> {mineral water}, {ground beef} -> {mineral water}, {ground beef} -> {spaghetti}, {chocolate} -> {mineral water}, {frozen vegetables} -> {mineral water}, {cake} -> {mineral water}, {soup} -> {mineral water}, {olive oil} -> {spaghetti}]\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd \n",
    "import numpy as np\n",
    "\n",
    "#数据加载\n",
    "data = pd.read_csv('/Users/yangkaixiang/Downloads/code/MarketBasket/Market_Basket_Optimisation.csv',header=None)\n",
    "print(data.head())\n",
    "print(data.shape)\n",
    "\n",
    "#将数据放到trasaction\n",
    "trasactions = []\n",
    "for i in range(data.shape[0]): #data.shape[0]=7501\n",
    "    temp = []\n",
    "    for j in range(data.shape[1]):#data.shape[1]=20\n",
    "        if str(data.values[i,j]) != 'nan':\n",
    "            temp.append(str(data.values[i,j]))\n",
    "    trasactions.append(temp)\n",
    "print(trasactions[2])\n",
    "\n",
    "from efficient_apriori import apriori\n",
    "itemsets,rules = apriori(trasactions,min_support=0.02,min_confidence=0.3)\n",
    "\n",
    "print(\"频繁项集:\",itemsets)\n",
    "print(\"关联规则:\",rules)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
